<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="银行管理员" prop="bankMemberName" >
        <el-input
          v-model="queryParams.bankMemberName"
          placeholder="请输入银行管理员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理银行名称" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入管理银行名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="企业id" prop="enterpriseId">
        <el-input
          v-model="queryParams.enterpriseId"
          placeholder="请输入企业id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="贷款用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入贷款用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户电话" prop="userNamePhone">
        <el-input
          v-model="queryParams.userNamePhone"
          placeholder="请输入用户电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dgw:manager:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dgw:manager:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dgw:manager:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['dgw:manager:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managerList.slice((pageNum-1)*pageSize,pageNum*pageSize)"  @selection-change="handleSelectionChange" >

      <el-table-column toggleRowExpansion="row" fixed type="expand">
        <template slot-scope="scope">
          <el-table :data="scope.row.tagNumberList" style="width: 100%"
                    height="400px"
                    :row-class-name="tableRowClassName">
            <el-table-column label="耳标编号" align="center" prop="earNumber"/>
            <el-table-column label="温度" align="center" prop="temp">
              <template slot-scope="scope">
                <el-tag effect="dark" size="small" :type="scope.row.temp< 20?'danger': 27<=scope.row.temp <=38?'primary':'danger'">
                  {{scope.row.temp}}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="计步" align="center" prop="jbu"/>
            <el-table-column label="电量" align="center" prop="power">
              <template slot-scope="scope">
                <el-tag effect="dark" size="small" :type="scope.row.power >=70?'primary': 50<scope.row.power <70?'warning':'danger'">
                  {{scope.row.power}}
                </el-tag>
              </template>
            </el-table-column>

            <el-table-column label="是否拆卸" align="center" prop="fcai">
              <template slot-scope="scope">
                <el-tag :type="scope.row.fcai==0?'primary':'danger'">
                  {{scope.row.fcai==0?'已拆卸':'未拆卸'}}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="用户手机号" align="center" prop="phone"/>
            <el-table-column label="牛品种" align="center" prop="cowType">
              <template slot-scope="scope">
                <span>{{scope.row.cowType==1?'育肥牛':scope.row.cowType==2?'牛犊':scope.row.cowType==3?'奶牛':'母牛'}}</span>
              </template>
            </el-table-column>
            <el-table-column label="牛棚名称" align="center" prop="cowShedName"/>
            <el-table-column label="牛棚地址" align="center" prop="shedAddress" width="300"/>
          </el-table>
        </template>
      </el-table-column>
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="银行管理_主键ID" align="center" prop="id" />-->
      <el-table-column label="贷款人名称" align="center" prop="enterpriseName" />
<!--      <el-table-column label="企业法人" align="center" prop="enterprisePerson" />-->
      <el-table-column label="联系电话" align="center" prop="enterpriseTel" />
<!--      <el-table-column label="贷款种类 1政府贷款,2用户贷款3企业贷款" align="center" prop="loansType" />-->
      <el-table-column label="贷款金额" align="center" prop="loanAmount" />
      <el-table-column label="牛总数" align="center" prop="cowSum" />
<!--      <el-table-column label="用户id" align="center" prop="enrollId" />-->
      <el-table-column label="审核状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag effect="dark" size="small" :type="scope.row.status ==1?'warning':scope.row.status ==2?'primary':'danger'">
            {{scope.row.status==1?'未审核':scope.row.status==2?'审核通过':'驳回'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="银行管理员" align="center" prop="bankMemberName" />
      <el-table-column label="管理银行名称" align="center" prop="bankName" />
      <el-table-column label="用户名称" align="center" prop="nickName" />
<!--      <el-table-column label="企业id" align="center" prop="enterpriseId" />-->
<!--      <el-table-column label="贷款用户" align="center" prop="userName" />-->
<!--      <el-table-column label="用户电话" align="center" prop="userNamePhone" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dgw:manager:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dgw:manager:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[15, 20, 30, 40, 50]"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改银行管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="养殖企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入养殖企业名称" />
        </el-form-item>
        <el-form-item label="企业法人" prop="enterprisePerson">
          <el-input v-model="form.enterprisePerson" placeholder="请输入企业法人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="enterpriseTel">
          <el-input v-model="form.enterpriseTel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="贷款种类 1政府贷款,2用户贷款3企业贷款" prop="loansType">
          <el-select v-model="form.loansType" placeholder="请选择贷款种类 1政府贷款,2用户贷款3企业贷款">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="贷款金额" prop="loanAmount">
          <el-input v-model="form.loanAmount" placeholder="请输入贷款金额" />
        </el-form-item>
        <el-form-item label="牛总数" prop="cowSum">
          <el-input v-model="form.cowSum" placeholder="请输入牛总数" />
        </el-form-item>
        <el-form-item label="用户id" prop="enrollId">
          <el-input v-model="form.enrollId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="银行管理员" prop="bankMemberName">
          <el-input v-model="form.bankMemberName" placeholder="请输入银行管理员" />
        </el-form-item>
        <el-form-item label="管理银行名称" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入管理银行名称" />
        </el-form-item>
        <el-form-item label="企业id" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入企业id" />
        </el-form-item>
        <el-form-item label="贷款用户" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入贷款用户" />
        </el-form-item>
        <el-form-item label="用户电话" prop="userNamePhone">
          <el-input v-model="form.userNamePhone" placeholder="请输入用户电话" />
        </el-form-item>
        <el-divider content-position="center">耳标信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTagNumber">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTagNumber">删除</el-button>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManager, getManager, delManager, addManager, updateManager, exportManager } from "@/api/dgw/manager";

export default {
  name: "Manager",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTagNumber: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 银行管理表格数据
      managerList: [],
      // 耳标表格数据
      tagNumberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      pageNum: 1,
      pageSize: 15,
      queryParams: {

        bankMemberName: null,
        bankName: null,
        enterpriseId: null,
        userName: null,
        userNamePhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /*表格颜色*/
    tableRowClassName({row, rowIndex}) {
      if (rowIndex !== -1) {
        return 'success-row';
      }
      return 'success-row';
    },
    /** 查询银行管理列表 */
    getList() {
      this.loading = true;
      listManager(this.queryParams).then(response => {
        console.log("查询列表：",response)
        this.managerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        enterprisePerson: null,
        enterpriseTel: null,
        loansType: null,
        loanAmount: null,
        createTime: null,
        updateTime: null,
        cowSum: null,
        enrollId: null,
        status: 0,
        bankMemberName: null,
        bankName: null,
        enterpriseId: null,
        userName: null,
        userNamePhone: null,
        createBy: null,
        updateBy: null
      };
      this.tagNumberList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加银行管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getManager(id).then(response => {
        this.form = response.data;
        this.tagNumberList = response.data.tagNumberList;
        this.open = true;
        this.title = "修改银行管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tagNumberList = this.tagNumberList;
          if (this.form.id != null) {
            updateManager(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManager(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除银行管理编号为"' + ids + '"的数据项？').then(function() {
        return delManager(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 耳标序号 */
    rowTagNumberIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 耳标添加按钮操作 */
    handleAddTagNumber() {
      let obj = {};
      obj.imei = "";
      obj.earNumber = "";
      obj.userId = "";
      obj.phone = "";
      obj.isUse = "";
      obj.enterpriseId = "";
      obj.insuranceId = "";
      this.tagNumberList.push(obj);
    },
    /** 耳标删除按钮操作 */
    handleDeleteTagNumber() {
      if (this.checkedTagNumber.length == 0) {
        this.$modal.msgError("请先选择要删除的耳标数据");
      } else {
        const tagNumberList = this.tagNumberList;
        const checkedTagNumber = this.checkedTagNumber;
        this.tagNumberList = tagNumberList.filter(function(item) {
          return checkedTagNumber.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTagNumberSelectionChange(selection) {
      this.checkedTagNumber = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有银行管理数据项？').then(() => {
        this.exportLoading = true;
        return exportManager(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: oldlace;
}
</style>
